package com.qifan.alb2b.service.resource;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.qifan.alb2b.action.back.param.HotelQueryParam;
import com.qifan.alb2b.dao.CityInfoDao;
import com.qifan.alb2b.dao.HotelInfoDao;
import com.qifan.alb2b.domain.HotelInfoDomain;
import com.qifan.alb2b.domain.ShoppingInfoDomain;
import com.qifan.alb2b.domain.TravelItemInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.route.SevenStPrice;
import com.qifan.alb2b.service.route.SixStPrice;
import com.qifan.alb2b.service.vo.Resource;
import com.qifan.alb2b.service.vo.ResourceVo;
import com.qifan.alb2b.service.vo.RouteDetailVo;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.service.vo.StPriceTable;
import com.qifan.alb2b.util.Arith;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ExchangeUtil;
import com.qifan.alb2b.util.Page;
import com.qifan.alb2b.util.Stringer;

/**
 * 酒店资源
 * @author hc24
 *
 */
@Component
public class HotelResource implements IResource {
	private static String[] header=new String[]{"ID","酒店名称","价格/人"};
	
	private HotelInfoDao hotelInfoDao;
	private CityInfoDao cityInfoDao;
	
	public void setHotelInfoDao(HotelInfoDao hotelInfoDao) {
		this.hotelInfoDao = hotelInfoDao;
	}

	public void setCityInfoDao(CityInfoDao cityInfoDao) {
		this.cityInfoDao = cityInfoDao;
	}


	public String[] getHeader(){
		return header;
	}
	
	public boolean isSingleSelect(){
		return true;
	}
	/**
	 * 酒店   价格/人
	 */
	public ResTable getResTable(String cityId,int groupType,DataGridModel dgm) {
		//根据城市信息查询酒店信息
		HotelQueryParam hqp=new HotelQueryParam();
		hqp.setCityId(cityId);
		dgm.setSort("stPrice");//设置排序字段
		dgm.setOrder("asc");//设置排序规则
		Page page= hotelInfoDao.query(dgm, hqp);
		List<HotelInfoDomain> list=page.getRows();
		
		//获得城市的标准配套价格
		List<Map<String, Object>> mapList=cityInfoDao.queryCityStdInfo(new String[]{cityId});
		if(mapList.size()==0)
			throw new BusinessException("设置酒店信息失败，该目的城市不存在");
		Map<String, Object> carStMap=mapList.get(0);
		String countryId=carStMap.get("countryId").toString();
		double hotelPrice= carStMap.get("hotelPrice")==null?0.0d:Double.parseDouble(carStMap.get("hotelPrice").toString());
		hotelPrice=ExchangeUtil.changeMoney(hotelPrice, countryId);//汇率转换

		//封装表格每行的数据
		List<Map<String, Object>> datas=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			HotelInfoDomain hotel=list.get(i);
			Map<String, Object> map=new LinkedHashMap<String, Object>();
			map.put(header[0], hotel.getHotelId());
			map.put(header[1], hotel.getHotelName());//酒店名称
			if(hotel.getHotelId().equals(carStMap.get("hotelId").toString())){				
				map.put(header[2], "[+0]起");//价格/人
			}else{
				double newHotelPrice=ExchangeUtil.changeMoney(hotel.getStPrice(), countryId);
				double diffPrice=Arith.sub(Arith.div(newHotelPrice, 2,Constants.XIAOSHU),Arith.div(hotelPrice,2,Constants.XIAOSHU));				
				String strDiff="[+"+Stringer.formatNumber(diffPrice,"0")+"]起";
				if(diffPrice<0){
					strDiff="["+Stringer.formatNumber(diffPrice,"0")+"]起";
				}
				map.put(header[2], strDiff);
			}
			map.put("查看详情", "<a href='javascript:void(0);' onclick=\"showDetail('"+cityId+"',"+Constants.RES_TYPE_HOTEL+",'"+hotel.getHotelId()+"')\">查看详情</a>");
			datas.add(map);		
		}
		page.setRows(datas);//重新设置分页的数据
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);
		rt.setSingleSelect(true);
		return rt;
	}

	/**
	 * 基本描述信息
	 */
	public ResTable getResDetailTable(String cityId,String resId,int groupType) {
		
		HotelInfoDomain hotel=hotelInfoDao.getHotelById(resId);
		if(hotel==null)
			throw new BusinessException("该酒店不存在，请联系管理员");
		
		ResTable rt=new ResTable();
		rt.setPage(new Page());
		
		rt.addRemark("酒店星级", hotel.getHotelLevel()+"");
		rt.addRemark("酒店位置", hotel.getHotelLocation());
		rt.addRemark("酒店地址", hotel.getHotelAddr());
		rt.addRemark("联系电话", hotel.getHotelTel());
		rt.addRemark("描述", hotel.getRemark());
		rt.setSingleSelect(false);
		return rt;
	}

	public void addOrUpdate(RouteVo routeVo, String routeDetailId, String[] resIds) {
//		if(resIds==null||resIds.length==0)
//			throw new BusinessException("标配资源必须选择");
		if(resIds==null)
			throw new BusinessException("标配资源必须选择");
		//=====查找线路详情=====
		List<RouteDetailVo> detailList= routeVo.getDetailVoList();
		RouteDetailVo routeDetailVo = null;
		for(RouteDetailVo rdv:detailList){
			if(rdv.getRouteDetail().getDetailId().equals(routeDetailId)){
				routeDetailVo=rdv;
				break;
			}
		}
		
		if(routeDetailVo==null)
			return;
		//=====更新接送资源resList=====
		String countryId=routeVo.getRoute().getCountryId();
		HotelInfoDomain oldHotel=null;
		HotelInfoDomain newHotel=null;
		List<ResourceVo> resList=routeDetailVo.getResList();
		
		if(resIds==null||resIds.length==0){//①代表清除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_HOTEL){
					oldHotel= (HotelInfoDomain) rv.getResource();//旧的
					newHotel=new HotelInfoDomain();//把新对象赋值给一个“0”对象
					newHotel.setHotelId("");
					newHotel.setStPrice(0);
					resList.remove(i);//移除
					break;
				}//end if
			}//end for
		}else{//②代表增加或者更新
			//首先把旧的全部移除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_HOTEL){
					oldHotel=(HotelInfoDomain) rv.getResource();
					resList.remove(i);//移除
					break;
				}//end if
			}//end for
			if(oldHotel==null){//如果没有旧对象，则赋值成“0”对象
				oldHotel=new HotelInfoDomain();
				oldHotel.setHotelId("");
				oldHotel.setStPrice(0);
			}
			//其次把新的全部加进来
			newHotel=hotelInfoDao.getHotelById(resIds[0]);
			ResourceVo rv=new ResourceVo();
			rv.setIsStd(1);
			rv.setResId(newHotel.getHotelId());
			newHotel.setStPrice((float) ExchangeUtil.changeMoney(newHotel.getStPrice(),countryId));//在这里就把价格转化成人民币!!!
			rv.setResource(newHotel);
			rv.setResType(Constants.RES_TYPE_HOTEL);
			resList.add(rv);
			
		}
		
		
		
//		for(int i=0;i<resList.size();i++){
//			ResourceVo rv=resList.get(i);
//			if(rv.getResType()==Constants.RES_TYPE_HOTEL){
//				if(!rv.getResId().equals(resIds[0])){
//					oldHotel=(HotelInfoDomain) rv.getResource();
//					rv.setResId(resIds[0]);
//					newHotel.setStPrice(ExchangeUtil.changeMoney(newHotel.getStPrice(),countryId));//存储汇率转化后的价格
//					rv.setResource(newHotel);
//					break;
//				}//end if
//			}//end if
//		}//end for
//		
//		if(oldHotel==null)
//			return;
		//=====更新标配价格表StTable=====
		StPriceTable spt=routeVo.getStPriceTable();
		List<double[]> datas= spt.getDatas();
		String[] rowType=spt.getRowTypes();
		int[] persons_num = null;
		int groupType=routeVo.getGroupType();
		if(groupType==Constants.GROUP_TYPE_SIX){
			persons_num=SixStPrice.PERSON_NUMS;
		}else if(groupType==Constants.GROUP_TYPE_SEVEN){
			persons_num=SevenStPrice.PERSON_NUMS;
		}
		
		double newP=newHotel.getStPrice();
		double oldP=oldHotel.getStPrice();
		double newCarPrice=Arith.div(newP, 2,Constants.XIAOSHU);
		double oldCarPrice=Arith.div(oldP,2,Constants.XIAOSHU);
		double diffPrice=Arith.sub(newCarPrice,oldCarPrice);
		double diffSingle=Arith.sub(Arith.roundCeiling(newP, Constants.XIAOSHU),Arith.roundCeiling(oldP, Constants.XIAOSHU));
		for(int i=0;i<rowType.length;i++){//每行	
			double[] rows=datas.get(i);
			for(int j=0;j<rows.length;j++){//没列
				if(rowType[0].equals("儿童占床")||rowType[0].equals("加床")||rowType[0].equals("双人间")){
					rows[j]=Arith.add(rows[j], diffPrice);
				}else if(rowType[0].equals("单人间")){
					rows[j]=Arith.add(rows[j], diffSingle);
				}
			}//end for j			
		}//end for i
	}

	public void calcPrice(TravelItemInfoDomain travelItem, Resource resource,
			int personNum) {
		HotelInfoDomain hotel=(HotelInfoDomain) resource;
		double price=Arith.div(hotel.getStPrice(), 2,Constants.XIAOSHU);//酒店标准价格/2
		double price2=Arith.roundCeiling(hotel.getStPrice(), Constants.XIAOSHU);
		travelItem.setPrice(price);//双人间、儿童加床的价格；对于儿童不加床是不加钱的。
		travelItem.setRemark("单间价格："+price2);
	}
	
	
}
	
	 


